using System.Collections;
using System.Collections.Generic;
public class EventManager
{
	protected List<IEventListener> listeners = null;
	protected bool swallow = false;

	public void SetSwallow(bool swallow)
	{
		this.swallow = swallow;
	}

	public EventManager ()
	{
		listeners = new List<IEventListener>();
	}

	protected int SortListener(IEventListener e1, IEventListener e2)
	{
		return e1.Priority()-e2.Priority();
	}


	public void BroadcastEvent(EventType type, EventTelegam tel)
	{
		swallow = false;
		for(int i=0;i<listeners.Count;++i)
		{
			if(swallow)
				break;
			listeners[i].OnReceiveMsg(type,tel);
		}
		swallow = false;
	}

	public void SendEvent(string listenerID, EventType type, EventTelegam tel)
	{
		for(int i=0;i<listeners.Count;++i)
		{
			if(listeners[i].ListenerID() == listenerID)
			{
				listeners[i].OnReceiveMsg(type,tel);
			}
		}
	}

	public void AddListener(IEventListener listener)
	{
		if(!listeners.Contains(listener))
		{
			listeners.Add(listener);
			listeners.Sort(this.SortListener);
		}

	}

	public void RemoveListener(IEventListener listener)
	{
		if(listeners.Contains(listener))
		{
			listeners.Remove(listener);
			listeners.Sort(this.SortListener);
		}
	}
}


